import {
    Component,
    OnInit,
    Injector,
    ViewChild,
    ViewEncapsulation,
} from "@angular/core";
import {
    ParkGuardServiceProxy,
    ParkGuardListDto,
    CreateParkingDto,
    UpdateParkingDto,
} from "@shared/service-proxies/service-proxies";
import * as moment from "moment";
import { LazyLoadEvent } from "primeng/components/common/lazyloadevent";
import { Paginator } from "primeng/components/paginator/paginator";
import { Table } from "primeng/components/table/table";

import { finalize } from "rxjs/operators";

import { AppComponentBase } from "@shared/common/app-component-base";
import { appModuleAnimation } from "@shared/animations/routerTransition";
import { ActivatedRoute, Router } from "@angular/router";
import { CreateOrEditParkingGuardModalComponent } from "./create-or-edit-parking-guard-modal.component";

@Component({
    selector: "app-park-guard",
    templateUrl: "./park-guard.component.html",
    styleUrls: ["./park-guard.component.css"],
    animations: [appModuleAnimation()],
})
export class ParkGuardComponent extends AppComponentBase implements OnInit {
    @ViewChild("dataTable") dataTable: Table;
    @ViewChild("paginator") paginator: Paginator;
    @ViewChild("createOrEditParkingGuradModal")
    createOrEditParkingGuradModal: CreateOrEditParkingGuardModalComponent;
    constructor(
        injector: Injector,
        private route: Router,
        private activeRoute: ActivatedRoute,
        private _parkGuardServiceProxy: ParkGuardServiceProxy
    ) {
        super(injector);
    }
    parkingid: number;
    parkingName: string;
    parkingCoce: string;
    neighbourhood_Name: string;
    neighbourhood_id: number;
    ngOnInit() {
        this.activeRoute.queryParams.subscribe((params) => {
            this.parkingid = params["parkingid"];
            this.parkingName = params["parkingName"];
            this.parkingCoce = params["parkingCoce"];
            this.neighbourhood_Name = params["neighbourhood_Name"];
            this.neighbourhood_id = params["neighbourhood_id"];
        });

        this.retuen();
    }

    getParkingGuard(event?: LazyLoadEvent): void {
        this._parkGuardServiceProxy
            .getAll(
                this.parkingid,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getSkipCount(this.paginator, event),
                this.primengTableHelper.getMaxResultCount(this.paginator, event)
            )
            .pipe(
                finalize(() => this.primengTableHelper.hideLoadingIndicator())
            )
            .subscribe((result) => {
                console.log(result);
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.totalRecordsCount = result.items.length;
                this.primengTableHelper.hideLoadingIndicator();
                console.log(result.items);
            });
    }
    deleteParkingGuard(ParkGuard: ParkGuardListDto): void {
        this.message.confirm(
            this.l("删除岗亭" + ParkGuard.guradName + "?"),
            this.l("AreYouSure"),
            (isConfirmed) => {
                if (isConfirmed) {
                    this._parkGuardServiceProxy
                        .delete(ParkGuard.id)
                        .subscribe(() => {
                            this.getParkingGuard();
                            abp.notify.success(this.l("SuccessfullyDeleted"));
                        });
                }
            }
        );
    }

    palceSet(ParkGuard: ParkGuardListDto): void {
        let parkingName = ParkGuard.parkingName;
        let parkingid = ParkGuard.parkingId;
        let parkingCoce = ParkGuard.parkingCode;
        let prakGruardId = ParkGuard.id;
        let prakGruardName = ParkGuard.guradName;
        let prakGruardCode = ParkGuard.guardNo;
        let neighbourhood_id = ParkGuard.neighbourhood_id;
        let neighbourhood_Name = ParkGuard.neighbourhood_name;
        this.route.navigateByUrl(
            "app/vehicl-management/devices?parkingid=" +
                parkingid +
                "&parkingName=" +
                parkingName +
                "&parkingCoce=" +
                parkingCoce +
                "&neighbourhood_id=" +
                neighbourhood_id +
                "&neighbourhood_Name=" +
                neighbourhood_Name +
                "&prakGruardId=" +
                prakGruardId +
                "&prakGruardName=" +
                prakGruardName +
                "&prakGruardCode=" +
                prakGruardCode
        );
    }
    retuen(): void {
        if (
            this.parkingid == undefined &&
            this.parkingName == undefined &&
            this.parkingCoce == undefined
        ) {
            this.route.navigateByUrl("app/vehicl-management/parkings");
            this.notify.error("页面访问错误");
        }
    }
}
